#!/usr/bin/env node

const Database = require('better-sqlite3');
const db = new Database('./data/delivery-management.db');

console.log('🔧 创建测试实例数据...\n');

try {
  // 为仪表-0015创建测试实例
  const deviceCode = '仪表-0015';
  const tableName = 'device_instances_instrumentation_controller';

  console.log(`为设备 ${deviceCode} 创建测试实例...`);

  // 1. 首先在参数表中创建测试数据
  console.log('\n1. 创建参数表数据:');

  // 创建命名表数据
  const namingInsert = db.prepare(`
    INSERT INTO table_c2 (设计物资编码, 采购物资编码, 物流编码, 施工物资编码, 运行资产编码)
    VALUES (?, ?, ?, ?, ?)
  `);
  const namingResult = namingInsert.run('YB-2025-001', 'CG-2025-001', 'WL-2025-001', 'SG-2025-001', 'YX-2025-001');
  const namingRecordId = namingResult.lastInsertRowid;
  console.log(`  命名记录ID: ${namingRecordId}`);

  // 创建位置表数据
  const positionInsert = db.prepare(`
    INSERT INTO table_c6 (设计设备位号_管线号, 工厂地址, 施工设备位号, 运行设备位号)
    VALUES (?, ?, ?, ?)
  `);
  const positionResult = positionInsert.run('1101-FC-001', '石楼油库控制室', '1101-FC-001A', '1101-FC-001');
  const positionRecordId = positionResult.lastInsertRowid;
  console.log(`  位置记录ID: ${positionRecordId}`);

  // 创建基础参数表数据
  const baseParamsInsert = db.prepare(`
    INSERT INTO table_c348 (规格, 型号, 制造厂家, 仪表名称, 准确度)
    VALUES (?, ?, ?, ?, ?)
  `);
  const baseParamsResult = baseParamsInsert.run('DN50', 'FC-3000', '某某自控设备有限公司', '批量控制器', '±0.1%');
  const baseParamsRecordId = baseParamsResult.lastInsertRowid;
  console.log(`  基础参数记录ID: ${baseParamsRecordId}`);

  // 2. 创建实例数据
  console.log('\n2. 创建实例数据:');
  const instanceInsert = db.prepare(`
    INSERT INTO ${tableName} (
      device_type_code, instance_number, object_number, object_description,
      naming_record_id, position_record_id, base_params_record_id,
      created_at, updated_at
    ) VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
  `);

  const instanceResult = instanceInsert.run(
    deviceCode,
    '001',
    '1101-FC-001',
    '原油装车批量控制器',
    namingRecordId,
    positionRecordId,
    baseParamsRecordId
  );

  console.log(`  实例ID: ${instanceResult.lastInsertRowid}`);
  console.log(`  实例编号: 001`);
  console.log(`  对象编号: 1101-FC-001`);

  // 3. 验证创建的数据
  console.log('\n3. 验证创建的数据:');
  const instance = db.prepare(`
    SELECT * FROM ${tableName} WHERE device_type_code = ?
  `).get(deviceCode);

  console.log('实例数据:');
  console.log(`  ID: ${instance.id}`);
  console.log(`  设备类型: ${instance.device_type_code}`);
  console.log(`  实例编号: ${instance.instance_number}`);
  console.log(`  对象编号: ${instance.object_number}`);
  console.log(`  对象描述: ${instance.object_description}`);
  console.log(`  命名记录ID: ${instance.naming_record_id}`);
  console.log(`  位置记录ID: ${instance.position_record_id}`);
  console.log(`  基础参数记录ID: ${instance.base_params_record_id}`);

  // 验证关联的参数数据
  if (instance.naming_record_id) {
    const namingData = db.prepare(`SELECT * FROM table_c2 WHERE id = ?`).get(instance.naming_record_id);
    console.log('\n关联的命名数据:');
    Object.keys(namingData).forEach(key => {
      if (key !== 'id' && key !== 'created_at' && key !== 'updated_at' && namingData[key] !== null) {
        console.log(`  ${key}: ${namingData[key]}`);
      }
    });
  }

  if (instance.position_record_id) {
    const positionData = db.prepare(`SELECT * FROM table_c6 WHERE id = ?`).get(instance.position_record_id);
    console.log('\n关联的位置数据:');
    Object.keys(positionData).forEach(key => {
      if (key !== 'id' && key !== 'created_at' && key !== 'updated_at' && positionData[key] !== null) {
        console.log(`  ${key}: ${positionData[key]}`);
      }
    });
  }

  if (instance.base_params_record_id) {
    const baseParamsData = db.prepare(`SELECT * FROM table_c348 WHERE id = ?`).get(instance.base_params_record_id);
    console.log('\n关联的基础参数数据:');
    Object.keys(baseParamsData).forEach(key => {
      if (key !== 'id' && key !== 'created_at' && key !== 'updated_at' && baseParamsData[key] !== null) {
        console.log(`  ${key}: ${baseParamsData[key]}`);
      }
    });
  }

  console.log('\n✅ 测试实例创建完成');

} catch (error) {
  console.error('❌ 创建测试实例失败:', error);
} finally {
  db.close();
}